package org.linlinjava.litemall.core.util;

import org.linlinjava.litemall.db.util.Str;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;

/**
 * 日期格式化工具类
 */
public class DateTimeUtil {

    public static Long SECOND_FOR_DAY = 5 * 60L;
    public static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static DateTimeFormatter YYYY_MM_DD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static DateTimeFormatter YYYYMMDD2 = DateTimeFormatter.ofPattern("yyyy.MM.dd");
    public static DateTimeFormatter YYYY_MM = DateTimeFormatter.ofPattern("yyyy-MM");
    public static DateTimeFormatter MM_DD = DateTimeFormatter.ofPattern("MM月dd日");


    /**
     * 格式 yyyy年MM月dd日 HH:mm:ss
     *
     * @param dateTime
     * @return
     */
    public static String getDateTimeDisplayString(LocalDateTime dateTime) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String strDate2 = dtf2.format(dateTime);
        return strDate2;
    }

    public static LocalDateTime formatYYYY_MM_DD(String formatStr) {
        if (Str.isEmpty(formatStr))
            return null;
        LocalDate now = LocalDate.parse(formatStr);
        return now.atStartOfDay();
    }


    public static LocalDateTime getStartOfDay(int offset) {
        LocalDate now = LocalDate.now().plusDays(offset);
        return now.atStartOfDay();
    }

    public static LinkedList<LocalDate> getWeek4() {
        LocalDate now = LocalDate.now();
        now = now.plusDays(4);
        DayOfWeek dayOfWeek = now.getDayOfWeek();
        int offset = dayOfWeek.getValue();
        int N = 4;
        if (offset == 7) {
            offset = 0;
        }
        LinkedList<LocalDate> list = new LinkedList<>();
        now = now.plusDays(1 - offset);
        while (N-- > 0) {
            LocalDate a = now;
            LocalDate b = a.plusDays(5);
            list.add(a);
            list.add(b);
            now = b.plusDays(2);
        }
        return list;
    }


}
